﻿using System;
using System.Data;
using System.Collections.Generic;
using DataLib.Common;
using DataLib.Model;
namespace DataLib.BLL
{
	/// <summary>
	/// 1
	/// </summary>
	public partial class VehicleArrange
	{
		private readonly DataLib.DAL.VehicleArrange dal=new DataLib.DAL.VehicleArrange();
		public VehicleArrange()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
			return dal.GetMaxId();
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int? id)
		{
			return dal.Exists(id);
		}

		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(DataLib.Model.VehicleArrange model)
		{
			return dal.Add(model);
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(DataLib.Model.VehicleArrange model)
		{
			return dal.Update(model);
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int? id)
		{
			
			return dal.Delete(id);
		}
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool DeleteList(string idlist )
		{
			return dal.DeleteList(idlist );
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public DataLib.Model.VehicleArrange GetModel(int? id)
		{
			
			return dal.GetModel(id);
		}

 
		/// <summary>
		/// 得到一个对象实体，从缓存中
		/// </summary>
		public DataLib.Model.VehicleArrange GetModelByCache(int? id)
		{
			
			string CacheKey = "VehicleArrangeModel-" + id;
			object objModel = DataLib.Common.DataCache.GetCache(CacheKey);
			if (objModel == null)
			{
				try
				{
					objModel = dal.GetModel(id);
					if (objModel != null)
					{
						int ModelCache = DataLib.Common.ConfigHelper.GetConfigInt("ModelCache");
						DataLib.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
					}
				}
				catch{}
			}
			return (DataLib.Model.VehicleArrange)objModel;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			return dal.GetList(strWhere);
		}


        public DataTable GetListByCity(string city)
        {
            return dal.GetListByCity(city);
        }
		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			return dal.GetList(Top,strWhere,filedOrder);
		}
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public List<DataLib.Model.VehicleArrange> GetModelList(string strWhere)
		{
			DataSet ds = dal.GetList(strWhere);
			return DataTableToList(ds.Tables[0]);
		}
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public List<DataLib.Model.VehicleArrange> DataTableToList(DataTable dt)
		{
			List<DataLib.Model.VehicleArrange> modelList = new List<DataLib.Model.VehicleArrange>();
			int rowsCount = dt.Rows.Count;
			if (rowsCount > 0)
			{
				DataLib.Model.VehicleArrange model;
				for (int n = 0; n < rowsCount; n++)
				{
					model = dal.DataRowToModel(dt.Rows[n]);
					if (model != null)
					{
						modelList.Add(model);
				}
				}
			}
			return modelList;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetAllList()
		{
			return GetList("");
		}

		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			return dal.GetRecordCount(strWhere);
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			return dal.GetListByPage( strWhere,  orderby,  startIndex,  endIndex);
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		//public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		//{
			//return dal.GetList(PageSize,PageIndex,strWhere);
		//}

		#endregion  BasicMethod
		#region  ExtensionMethod

        public DataLib.Model.VehicleArrange GetModelByCjoid(int cjoid)
        {
            return dal.GetModelByCjoid(cjoid);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DataLib.Model.VehicleArrange GetModelByOutid(int outid)
        {
            return dal.GetModelByOutid(outid);
        }

        public DataTable GetVehicleList(string city)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("l_loadnumno");
            dt.Columns.Add("c_cjonno");
            dt.Columns.Add("cum_code");
            dt.Columns.Add("cum_name");
            dt.Columns.Add("v_numberestimate");
            dt.Columns.Add("v_peculiarrequire");
            dt.Columns.Add("s_peculiarrequire");    //缩短后的要求
            dt.Columns.Add("s_Vehiclor");           //收货员缩略
            dt.Columns.Add("a_Vehiclor");           //全部收货员
            dt.Columns.Add("s_PlateNo");            //车牌缩略
            dt.Columns.Add("a_PlateNo");            //车牌全部
            dt.Columns.Add("vaid");                 //派车ID
            DataTable temp = new DataTable();
            
            temp = GetListByCity(city);
            if (temp != null && temp.Rows.Count > 0)
            {
                for (int i = 0; i < temp.Rows.Count; i++)
                {
                    DataRow dr = dt.NewRow();
                    GetRowsData(temp.Rows[i]["id"].ToString(), dr,city);
                    if (dr != null)
                    {
                        dt.Rows.Add(dr);
                    }
                    else
                    {
                        dt.Rows.Remove(dr);
                    }
                   
                }
            }
            return dt;
        }

        /// <summary>
        /// 逐行赋值
        /// </summary>
        /// <param name="vaid"></param>
        /// <param name="dr"></param>
        /// <returns></returns>
        public void GetRowsData(string vaid,DataRow dr,string city)
        {
 
            Model.VehicleArrange vmodel = GetModel(int.Parse(vaid));
              dr["vaid"] = vaid;
            if (vmodel.outid != null && vmodel.outid != 0)
            {
                Model.OutStorage oModel = new BLL.OutStorage().GetModel((int)vmodel.outid);
                if ((city!="") && (oModel.OutDepart != city))
                {
                    dr = null;
                    return;
                }
                dr["l_loadnumno"] = oModel.VLoadNum;
                dr["c_cjonno"] = oModel.outStorageNo;
                dr["v_numberestimate"] = "";
                dr["v_peculiarrequire"] = "";
                dr["s_peculiarrequire"] = "";
              
                if (oModel.StorageType != "原料库")
                {
                    if ((oModel.inDepart == oModel.belogarea) && (oModel.OutDepart != oModel.graphicCity))
                    {
                        dr["cum_code"] = oModel.graphicCity + oModel.OutDepart + "-Warehouse";
                        dr["cum_name"] = oModel.graphicCity + oModel.OutDepart + "-Warehouse";

                    }
                    else
                    {
                        dr["cum_code"] = oModel.OutDepart + "-TES";
                        dr["cum_name"] = oModel.OutDepart + "-TES";
                    }
                }
                else
                {
                    Model.LoadNum lModel = new LoadNum().GetLoadID(oModel.VLoadNum);
                    if (lModel != null)
                    {
                        Model.Cumtomer cumModel = new Cumtomer().GetModel((int)lModel.customerid);
                        dr["cum_code"] = cumModel.customercode;
                        dr["cum_name"] = cumModel.companyname;
                    }
                }
            }
            else
            {
                Model.CJOItem cmodel= new BLL.CJOItem().GetListByCid((int)vmodel.cjoid);
                if (city != "" && city != cmodel.Graphicy)
                {
                    dr = null;
                    return;
                }
                dr["l_loadnumno"] = cmodel.loadnumno;
                dr["c_cjonno"] = cmodel.cjono;
                dr["v_numberestimate"] = cmodel.numberestimate.ToString();
                dr["v_peculiarrequire"] = cmodel.peculiarrequire;
                if (cmodel.peculiarrequire != null && cmodel.peculiarrequire.Length > 7)
                    dr["s_peculiarrequire"] = cmodel.peculiarrequire.Substring(0, 5) + "...";
                else
                    dr["s_peculiarrequire"] = cmodel.peculiarrequire;
                dr["cum_code"] = cmodel.customerid;
                dr["cum_name"] = cmodel.companyname;
            }
            dr["a_PlateNo"] = getStrPlateNo(vmodel, 1);
            dr["s_PlateNo"] = getStrPlateNo(vmodel, 0); ;
            dr["a_Vehiclor"] = GetVehiclor(vmodel, 1);
            dr["s_Vehiclor"] = GetVehiclor(vmodel, 0);
        
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="vamodel"></param>
        /// <param name="flag">1为完全的，否则为缩略</param>
        /// <returns></returns>
        private string getStrPlateNo(Model.VehicleArrange vamodel,int flag)
        {
            string plateNumInfo = "";
            if (vamodel != null)
            {
                BLL.PlateNumber plateBll = new DataLib.BLL.PlateNumber();
                DataLib.Model.PlateNumber plateModel = new DataLib.Model.PlateNumber();
                string[] brr = vamodel.platenumberid.Split(',');
                if (brr.Length > 0)
                {
                    plateModel = plateBll.GetModel(int.Parse(brr[0]));
                    plateNumInfo += new BLL.TransportCompany().GetModel(plateModel.transportIid).companyname + "-" + plateModel.maxton + "-" + plateModel.plateno;

                    if (brr.Length > 1)
                    {
                        if (flag == 0)
                        {
                            plateNumInfo += "...";
                        }
                        else
                        {
                            for (int i = 1; i < brr.Length; i++)
                            {
                                if (brr[i] != null && brr[i].Trim() != "")
                                {
                                    plateNumInfo += ",";
                                    plateModel = plateBll.GetModel(int.Parse(brr[i]));
                                    plateNumInfo += new BLL.TransportCompany().GetModel(plateModel.transportIid).companyname + "-" + plateModel.maxton + "-" + plateModel.plateno;

                                }
                            }
                        }
                    }
                }

            }
            return plateNumInfo; 
        }

        /// <summary>
        /// 显示收货员名
        /// </summary>
        /// <param name="cjoid"></param>
        /// <param name="flag">0只取第一个，加省略号，1取多个，</param>
        /// <returns></returns>
        protected string GetVehiclor(Model.VehicleArrange vaodel, int flag)
        {
            string vehiclor = "";
            if (vaodel != null)
            {
                BLL.Vehiclor vBll = new DataLib.BLL.Vehiclor();
                DataLib.Model.Vehiclor vModel = new DataLib.Model.Vehiclor();
                string[] brr = vaodel.vehiderid.Split(',');
                if (brr.Length > 0)
                {
                    vModel = vBll.GetModel(int.Parse(brr[0]));
                    vehiclor += vModel.name;

                    if (brr.Length > 1)
                    {
                        if (flag == 0)
                        {
                            vehiclor += "...";
                        }
                        else
                        {
                            for (int i = 1; i < brr.Length; i++)
                            {
                                if (brr[i] != null && brr[i].Trim() != "")
                                {
                                    vehiclor += ",";
                                    vModel = vBll.GetModel(int.Parse(brr[i]));
                                    vehiclor += vModel.name;

                                }
                            }
                        }
                    }
                }

            }
            return vehiclor;

        }     

        
        /// <summary>
        /// 获取派车维护列表
        /// </summary>
        /// <param name="bcity">归属地</param>
        /// <param name="gcity">处理地</param>
        /// <returns></returns>
        public DataTable GetSendCarList(String bcity, String gcity)
        {
            return dal.GetSendCarList(bcity, gcity);
        }

        /// <summary>
        /// 根据CJOID或者OUTID获得对象
        /// </summary>
        /// <param name="srcid"></param>
        /// <param name="flag"></param>
        /// <returns></returns>
        public Model.VehicleArrange GetModelBySrcid(string srcid, int flag)
        {
            Model.VehicleArrange vModel= null;
            DataTable dt = new DataTable();
            if (flag == 1)
            {
                dt = GetList("cjoid=" + srcid).Tables[0];
            }
            else
            {
                dt = GetList("outid=" + srcid).Tables[0];
            }
            if(dt!=null && dt.Rows.Count>0)
                vModel = dal.DataRowToModel(dt.Rows[0]);
            return vModel;
        }


        public DataTable GetSendCarListStore(string bcity, string gcity, int currpage, int length, out int totalcount,
                                             string search)
        {
            return dal.GetSendCarListStore(bcity, gcity, currpage, length, out totalcount, search);
        }
		#endregion  ExtensionMethod
	}
}

